/*
 * jstree.directive [http://www.jstree.com]
 * http://arvindr21.github.io/jsTree-Angular-Directive
 *
 * Copyright (c) 2014 Arvind Ravulavaru
 * Licensed under the MIT license.
 */

var ngJSTree = angular.module('jsTree.directive', []);
ngJSTree.directive('jsTree', function($http) {
	
  var treeDir = {
    restrict: 'EA',
    fetchResource: function(url, cb) {
      return $http.get(url).then(function(data) {
        if (cb) cb(data.data);
      });
    },
    
    managePlugins: function(s, e, a, config) {
      if (a.treePlugins) {
        config.plugins = a.treePlugins.split(',');
        config.core = config.core || {};
        config.core.check_callback = true;

        if (config.plugins.indexOf('state') >= 0) {
          config.state = config.state || {};
          config.state.key = a.treeStateKey;
        }

        if (config.plugins.indexOf('search') >= 0) {
          var to = false;
          
          //搜索
          $('#searchInput').keyup(function(ev) {
        	  treeDir.tree.jstree(true).deselect_all();
        	  if(to) { 
        		  clearTimeout(to); 
        	  }
      	      to = setTimeout(function () {
      	    	treeDir.tree.jstree(true).search(ev.target.value);
      	    }, 250);
      	      
          });
          
//          e.after('<input type="text" placeholder="Search Tree" class="ng-tree-search"/>')
//            .next()
//            .on('keyup', function(ev) {
//              if (to) {
//                clearTimeout(to);
//              }
//              to = setTimeout(function() {
//                treeDir.tree.jstree(true).search(ev.target.value);
//              }, 250);
//            });
        }

        if (config.plugins.indexOf('checkbox') >= 0) {
          config.checkbox = config.checkbox || {};
          config.checkbox.keep_selected_style = false;
          //Add By Mary
          if (a.treeCheckbox) {
              config.checkbox = s[a.treeCheckbox];
              console.log(config);
            }
        }

        if (config.plugins.indexOf('contextmenu') >= 0) {
        	config.contextmenu = config.contextmenu || {};
          if (a.treeContextmenu) {
//            config.contextmenu.items = function() {
//            Edit By Mary
            config.contextmenu = s[a.treeContextmenu];
          }
        }

        if (config.plugins.indexOf('types') >= 0) {
          if (a.treeTypes) {
            config.types = s[a.treeTypes];
            console.log(config);
          }
        }
        
        if (config.plugins.indexOf('dnd') >= 0) {
            if (a.treeDnd) {
              config.dnd = s[a.treeDnd];
              console.log(config);
            }
          }
        
      }
      return config;
    },
    manageEvents: function(s, e, a) {
      if (a.treeEvents) {
        var evMap = a.treeEvents.split(';');
        for (var i = 0; i < evMap.length; i++) {
          if (evMap[i].length > 0) {
            var evt = evMap[i].split(':')[0];
            var cb = evMap[i].split(':')[1];
            if (evt.indexOf('.') < 0) {
                evt = evt + '.jstree';
                treeDir.tree.on(evt, s[cb]);
            }else if (evt.indexOf('.vakata')>=0) {
            	$(document).on(evt,s[cb]);
            }
            
          }
        }
      }
    },
    link: function(s, e, a) { // scope, element, attribute \O/
      $(function() {
        var config = {};
        // clean Case
        a.treeData = a.treeData ? a.treeData.toLowerCase() : '';
        a.treeSrc = a.treeSrc ? a.treeSrc.toLowerCase() : '';

        if (a.treeData == 'html') {
          treeDir.fetchResource(a.treeSrc, function(data) {
            e.html(data);
            treeDir.init(s, e, a, config);
          });
        } else if (a.treeData == 'json') {
          treeDir.fetchResource(a.treeSrc, function(data) {
            config = {
              'core': {
                'data': data
              }
            };
            treeDir.init(s, e, a, config);
          });
        } else if (a.treeData == 'scope') {
          s.$watch(a.treeModel, function() {
            config = {
              'core': {
                'data': s[a.treeModel]
              }
            };
            $(e).jstree('destroy');
            treeDir.init(s, e, a, config);
          }, true);
        } else if (a.treeAjax) {
          config = {
            'core': {
              'data': {
                'url': a.treeAjax,
                'data': function(node) {
                  return {
                    'id': node.id != '#' ? node.id : 1
                  };
                }
              }
            }
          };
          treeDir.init(s, e, a, config);
        }
      });

    },
    init: function(s, e, a, config) {
      treeDir.managePlugins(s, e, a, config);
      this.tree = $(e).jstree(config);
      treeDir.manageEvents(s, e, a);
    }
  };

  return treeDir;

});